
package com.qin.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qin.springcloud.bean.Users;
import com.qin.springcloud.service.UsersService;

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class UsersController {

	@Autowired
	private UsersService usersService;

	@HystrixCommand(fallbackMethod = "getByHystrix")
	@GetMapping("/users/get/{id}")
	public Users getById(@PathVariable int id) {
		System.out.println(id);
		Users users = usersService.getById(id);
		System.out.println(users);
		if (users == null) {
			throw new RuntimeException("id is not fond");
		}
		return users;
	}

	public Users getByHystrix(@PathVariable int id) {

		Users users = new Users();
		users.setName("new by hystrix");
		return users;

	}
	
	@HystrixCommand
	@GetMapping("/users/getByHystrix")
	public String getById(){
		throw new RuntimeException("id is not fond");
	}
	
	public String defaultFallback() {
		return "defaultFallback";
	}
}
